#include "stdafx.h"
#include "../Core/Main/Application.h"
#include "../Widgets/WdgLogEntry.h"
#include "../Widgets/WdgLogbookOptions.h"
#include "../Widgets/WdgLogbookToolbar.h"
#include "Logbook.h"

namespace ham
{
	Logbook::Logbook(QWidget* parent)
		: Module(parent),
		m_tabBarCloseButton(0)
	{
	}

	Logbook::~Logbook(void)
	{
	}
		
	QString	Logbook::getModuleName() const
	{
		return "Logbook";
	}

	QString Logbook::getDisplayName() const
	{
		return getModuleName();
	}

	bool Logbook::isInit() const
	{
		return true &&
			Module::isInit();
	}

	bool Logbook::iInit()
	{		
		setupUi(this);
		
		// add a default log entry
		addNewEntry(0);
		
		editButton->setChecked(false);
		showLogEntries(editButton->isChecked());

		// init database layout
		initDatabaseLayout();

		return true;
	}
	
	void Logbook::initDatabaseLayout()
	{
		QTableView* table = tableView;

		QStandardItemModel* model = new QStandardItemModel(this);
		model->setHorizontalHeaderItem(0, new QStandardItem("Date"));
		model->setHorizontalHeaderItem(1, new QStandardItem("Start"));
		model->setHorizontalHeaderItem(2, new QStandardItem("Callsign"));
		model->setHorizontalHeaderItem(3, new QStandardItem("Sent"));
		model->setHorizontalHeaderItem(4, new QStandardItem("Rcvd"));
		model->setHorizontalHeaderItem(5, new QStandardItem("Freq"));
		model->setHorizontalHeaderItem(6, new QStandardItem("Band"));
		model->setHorizontalHeaderItem(7, new QStandardItem("Mode"));
		model->setHorizontalHeaderItem(8, new QStandardItem("Name"));
		model->setHorizontalHeaderItem(9, new QStandardItem("Country"));
		model->setHorizontalHeaderItem(10, new QStandardItem("Locator"));
		model->setHorizontalHeaderItem(11, new QStandardItem("Distance"));
		model->setHorizontalHeaderItem(12, new QStandardItem("Comment"));
		
		table->setModel(model);
	}
	
	WdgOptions* Logbook::getOptionsWdg(QWidget* parent)
	{
		return new WdgLogbookOptions(&m_properties, parent);
	}
	
	QList<QMenu*> Logbook::getMenus(QWidget* parent) const
	{
		QList<QMenu*> menus;

		// add menu
		QMenu* menu = new QMenu(getModuleName(), parent);
		QAction* nothing = new QAction("No entries here", parent);
		menu->addAction(nothing);

		menus.push_back(menu);

		return menus;
	}

	QList<QPair<QString, QWidget*>> Logbook::getMainWidgets(QWidget* parent)
	{
		QList<QPair<QString, QWidget*>> mainWidgets;

		QPair<QString, QWidget*> wdgPair(getDisplayName(), this);
		mainWidgets.push_back(wdgPair);

		return mainWidgets;
	}
	
	QList<QPair<QString, QWidget*>> Logbook::getToolbarWidgets(QWidget* parent)
	{
		QList<QPair<QString, QWidget*>> toolbarWidgets;

		QPair<QString, QWidget*> wdgPair(getDisplayName(), new WdgLogbookToolbar(parent));
		toolbarWidgets.push_back(wdgPair);

		return toolbarWidgets;
	}
	
	void Logbook::addNewEntry(int index)
	{
		// create a new entry, if "+" tab is pressed
		if (index == tabWidget->count() - 1) {
			int position = tabWidget->count() - 1;
			WdgLogEntry* entry = new WdgLogEntry(this);

			// connect
			QObject::connect(entry, SIGNAL(tabNameChanged(QString, const WdgLogEntry*)),
				this, SLOT(changeTabName(QString, const WdgLogEntry*)));

			tabWidget->insertTab(position, entry, "New entry");
			tabWidget->setCurrentIndex(position);

			// remove close button on first tab
			if (tabWidget->count() <= 2) {
				m_tabBarCloseButton = tabWidget->tabBar()->tabButton(0, QTabBar::RightSide);
				tabWidget->tabBar()->setTabButton(0, QTabBar::RightSide, 0);
			}
			
			//tabWidget->tabBar()->tabButton(position, QTabBar::RightSide)->setFixedSize(12, 12);

			// remove close button on "+" tab
			tabWidget->tabBar()->setTabButton(position + 1, QTabBar::RightSide, 0);
					
			// restore close button on first tab
			if (tabWidget->count() > 2 && m_tabBarCloseButton) {
				tabWidget->tabBar()->setTabButton(0, QTabBar::RightSide, m_tabBarCloseButton);
				m_tabBarCloseButton = 0;
			}
		}
	}

	void Logbook::closeLogEntry(int index)
	{
		// find log entry and cancel changes
		QList<WdgLogEntry*> entries = tabWidget->findChildren<WdgLogEntry*>();
		for (int i = 0; i < entries.size(); i++) {
			if (i == index) {
				WdgLogEntry* entry = entries[i];
				entry->cancel();
				break;
			}
		}
		
		// select appropriate tab
		if (index == tabWidget->count() - 2)
			tabWidget->setCurrentIndex(index - 1);
		else if (tabWidget->count() > 1)
			tabWidget->setCurrentIndex(index);
		else if (tabWidget->count() <= 1)
			tabWidget->setCurrentIndex(0);
		
		// remove tab
		tabWidget->removeTab(index);
		
		// remove close button on first tab
		if (tabWidget->count() <= 2) {
			m_tabBarCloseButton = tabWidget->tabBar()->tabButton(0, QTabBar::RightSide);
			tabWidget->tabBar()->setTabButton(0, QTabBar::RightSide, 0);
		}
	}

	void Logbook::showLogEntries(bool checked)
	{
		if (checked)
			tabWidget->setVisible(true);
		else
			tabWidget->setVisible(false);
	}
	
	void Logbook::changeTabName(QString name, const WdgLogEntry* entry)
	{
		// UNDONE: does not work always with multiple tabs
		QList<WdgLogEntry*> entries = tabWidget->findChildren<WdgLogEntry*>();
		for (int i = 0; i < entries.size(); i++) {
			WdgLogEntry* curEntry = entries[i];
			if (curEntry == entry) {
				tabWidget->setTabText(i, name);
				break;
			}
		}
	}
}